#$Id$

package Feeds::GCF::Exception::ExcREODTrchBroker;
@ISA = qw(Feeds::GCF::Exception::ExcCCBroker);

use strict qw(vars);
use warnings ( FATAL => 'all' );

use Data::Dumper;
use Readonly;
use POSIX qw(strftime);

use Feeds::GCF::Constants qw(:GENERIC :STD_TABLE :STD_TYPE :SYBASE_SQL);
use Feeds::GCF::Util qw(hostname now today redo_sql);
use Feeds::Utils::CDSBrokerConfig;
use Feeds::Utils::CDSBrokerMapper;
use Feeds::GCF::Viper;
use Feeds::GCF::Exception::ExcCCBroker;

sub set_configuration {
    my $self = shift;
    my $dbh  = $self->{dbh};
    my $log  = $self->{log};

    $self->init_tranche_identifier();
}

sub __get_candidates {
    my $self = shift;
    my $type = shift;          #type can be sn,idx, tranche
    my $dbh  = $self->{dbh};
    my $log  = $self->{log};

    my $type_condition = $self->{xml_config}->{tranche_criterial};
    my $sql;

    if ( $self->{param}->{candidate_list}
        && scalar @{ $self->{param}->{candidate_list} } > 0 )
    {
        my $contract_list = join ",", @{ $self->{param}->{candidate_list} };
        $sql = "SELECT oid as contract_id , 'NVD' as status_cd
  	       FROM  $self->{xml_config}->{n_tbl}
  	       WHERE  oid in (" . $contract_list . ")";
    }
    else {

        my $top = '';
        if ( $self->{param}->{load_limit} ) {
            $top = " top $self->{param}->{load_limit} ";
        }
        $sql = <<"SQL";
        select t1.contract_id, 'NVD' as status_cd
    from p_ent_stat t1, $self->{xml_config}->{n_tbl} t2
    where t1.feed_type = '$self->{fmeta}->{feed_type}'
      and t1.contract_id = t2.oid
      and $type_condition
    order by t1.status_dt desc
SQL

    }

    $log->info("retrieve candiate sql:\n$sql");

    my $candidates = $dbh->selectall_hashes($sql)
        or $log->die( $dbh->errstr );
    return $candidates;
}

sub check_WTM {
    my $self   = shift;
    my $asset  = shift;
    my $n_data = shift;

    if ( !$n_data->{n_tbl}->{ $self->{xml_config}->{term_src_field} } ) {
        push @{ $asset->{exc_cd} }, 'WTM';
        return;
    }

    $asset->{mapping}->{contract}->{term}
        = $n_data->{n_tbl}->{ $self->{xml_config}->{term_src_field} } . 'Y';

    return;

}



sub check_tranche_tier_cd{
 
    my $self   = shift;
    my $asset  = shift;
    my $n_data = shift;
    
    
    $asset->{mapping}->{contract}->{attach}= $n_data->{n_tbl}->{tranche_attach};
    $asset->{mapping}->{contract}->{detach}= $n_data->{n_tbl}->{tranche_exhaust};
    
    $asset->{mapping}->{contract}->{idx_trch_leg_nm}
        = $n_data->{n_tbl}->{key_col} ;

    $asset->{mapping}->{contract}->{ric_tranche_cd}
        = $self->{mapper}->map_num_to_charater( $n_data->{n_tbl}->{key_col} );
    
    $asset->{mapping}->{contract}->{idx_trch_tier_cd}
        = $self->{tranche_tier_cd_def}->{ $asset->{mapping}->{contract}->{ric_tranche_cd}} ;    
    
}

sub init_tranche_identifier {
    my $self = shift;
    my $dbh  = $self->{dbh};
    my $log  = $self->{log};

    my $sql = <<"SQL";
  
    SELECT MAX(key_col)/2 as max_trch_cd,red_cd, tenor
      INTO #tmp_max_trch_cd
      FROM n_reodtranche 
    GROUP BY red_cd,tenor
      
SQL

    $dbh->do($sql) || $log->die("init_tranche_identifier failed");

}

1;
